package game;

import game.Room.Direction;

import java.util.List;


public class Player {
   
   private String name;
   private Room room;
   private Room home;
   private int turns = 20;
   private int count = 0;
   
   public Player(String name, Room room) {
      this.name = name;
      this.room = room;
      home = room;
      room.addPlayer(this);
   }
   
   public Room move(Direction d) {
      Room goRoom = room.get(d);
      if (goRoom == null) {
         System.out.println("Cannot move in this direction! " + d);
         return null;
      } else {
         count++;
      }
      room.removePlayer(this);
      room = goRoom;
      room.addPlayer(this);
      return room;
   }
   
   public void move(List<Character> dirs) {
      Room goRoom = null;
      for (char c : dirs) {
         if (c == 'N') {
            goRoom = move(Direction.NORTH);
         } else if (c == 'E') {
            goRoom = move(Direction.EAST);
         } else if (c == 'S') {
            goRoom = move(Direction.SOUTH);
         } else if (c == 'W') {
            goRoom = move(Direction.WEST);
         }
         if (goRoom == null) return;
      }
   }
   
   public Room getRoom() {
      return room;
   }
   
   public void setRoom(Room room) {
      this.room.removePlayer(this);
      this.room = room;
      room.addPlayer(this);
   }
   
   public String look() {
      String n = System.getProperty("line.separator");
      StringBuffer strBfr = new StringBuffer("You are in room " + room + n);
      if (room.getNorth() != null || room.getEast() != null || room.getSouth() != null
            || room.getWest() != null)strBfr.append("You can move:");
      if (room.getNorth() != null)strBfr.append(n+"north (" + room.getNorth() + ")");
      if (room.getEast() != null)strBfr.append(n+"east (" + room.getEast() + ")");
      if (room.getSouth() != null)strBfr.append(n+"south (" + room.getSouth() + ")");
      if (room.getWest() != null)strBfr.append(n+"west (" + room.getWest() + ")");
      if (room.getPlayers().size() > 1) {
         strBfr.append(n+"You see: ");
         for (Player p : room.getPlayers()) {
            if (p != this) {
               strBfr.append(p + ", ");
            }
         }
      }
      if (room.getBodies().size() > 0) {
         strBfr.append(n+"You see dead people: ");
         for (String p : room.getBodies()) {
            strBfr.append(p + ", ");
         }
      }
      return strBfr.toString();
   }
   
   public void remove() {
      room.removePlayer(this);
   }
   
   public void setBackToHome() {
      setRoom(home);
   }

   @Override
   public String toString() {
      return name;
   }

   public int getTurns() {
      return turns;
   }

   public int getCount() {
      return count;
   }
}
